package com.hongxun.modules.csg.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.common.validator.ValidatorUtils;
import com.hongxun.common.validator.group.UpdateGroup;
import com.hongxun.modules.csg.entity.CArrivalRecordEntity;
import com.hongxun.modules.csg.service.CArrivalRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.csg.entity.CGoodsDetailEntity;
import com.hongxun.modules.csg.service.CGoodsDetailService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 物资清单
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-11 09:30:52
 */
@RestController
@RequestMapping("/csg/cgoodsdetail")
public class CGoodsDetailController {
	@Autowired
	private CGoodsDetailService cGoodsDetailService;
	@Autowired
	private CArrivalRecordService cArrivalRecordService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		List<CGoodsDetailEntity> cGoodsDetailList = new ArrayList<CGoodsDetailEntity>();


		//堆放点-->物资清单【取消】2018-10-1 08:43:13
		/*if (params.get("sPGuid")!=null && !params.get("sPGuid").toString().equals("")) {
			//查询到货记录
			wheres += " and s_p_guid='"+params.get("sPGuid").toString()+"' and p_guid='"+params.get("pGuid").toString()+"'";
			query.put("filterSql",wheres+" ");
			List<CArrivalRecordEntity> cArrivalList = cArrivalRecordService.queryList(query);
			query.remove("filterSql");
			wheres = wheres.substring(0,4);

			//物资清单
			for (int i = 0; i < cArrivalList.size(); i++) {
				String guid = cArrivalList.get(i).getGuid();
				if (i == 0) {
					wheres += " and a_r_guid = '"+guid+"'";
				}else {
					wheres += " or a_r_guid = '"+guid+"'";
				}
			}
			query.put("filterSql",wheres+" ");
			cGoodsDetailList = cGoodsDetailService.queryList(query);
		}*/


		//到货记录-->物资清单
		if (params.get("guid")!=null && !params.get("guid").toString().equals(""))
		{
			wheres += " and a_r_guid='"+params.get("guid").toString()+"'";
		}
		if (params.get("gDName")!=null && !params.get("gDName").toString().equals(""))
		{
			wheres += " and g_d_name like'%"+params.get("gDName").toString()+"%'";
		}
		if (params.get("pGuid")!=null && !params.get("pGuid").toString().equals(""))
		{
			wheres += " and p_guid='"+params.get("pGuid").toString()+"'";
		}


		query.put("filterSql",wheres+" ");
		cGoodsDetailList = cGoodsDetailService.queryList(query);


		int total = cGoodsDetailService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(cGoodsDetailList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}


	/**
	 * 查询
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping("/select")
	public R select(@RequestParam Map<String , Object> params){

		String where = " 1=1 and g_d_datastate!='删除' ";
		Map<String , Object> map = new HashMap<>();
		map.put("filterSql",where);
		List<CGoodsDetailEntity> cGoodsDetailEntityList = cGoodsDetailService.queryList(map);

		return R.ok().put("returnlist", cGoodsDetailEntityList);
	}










	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		CGoodsDetailEntity cGoodsDetail = cGoodsDetailService.queryObject(guid);
		
		return R.ok().put("cGoodsDetail", cGoodsDetail);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody CGoodsDetailEntity cGoodsDetail){
		cGoodsDetail.setGDDatastate("新增");
		cGoodsDetailService.save(cGoodsDetail);
		
		return R.ok();
	}


	/**
	 * 新增物资
	 *
	 * @param cGoodsDetail
	 * @param guid
	 * @param pGuid
	 * @return
	 */
	@RequestMapping("/saveNew/{guid}/{pGuid}")
	public R save(@RequestBody CGoodsDetailEntity cGoodsDetail ,
				  @PathVariable String guid ,
				  @PathVariable String pGuid)
	{

		// TODO: 2018/9/19 新增物资时，通过sPGuid=aRGuid进行关联

		Map<String , Object> cArrivalRecordMap = new HashMap<>();

		String aRGuid = "";
		String whereCArrival = " 1=1 and s_p_guid='"+guid+"' ";
		cArrivalRecordMap.put("filterSql" , whereCArrival);
		List<CArrivalRecordEntity> cArrivalRecordEntityList = cArrivalRecordService.queryList(cArrivalRecordMap);

		//设值
		if (cArrivalRecordEntityList != null && cArrivalRecordEntityList.size() !=0) {
			for (CArrivalRecordEntity arrivalRecordEntity : cArrivalRecordEntityList) {
				aRGuid = arrivalRecordEntity.getGuid();
			}
			cGoodsDetail.setARGuid(aRGuid);
		}else{
			cGoodsDetail.setARGuid(guid);
		}

		cGoodsDetail.setPGuid(pGuid);
		cGoodsDetail.setGDDatastate("新增");
		cGoodsDetailService.save(cGoodsDetail);

		return R.ok();
	}


	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody CGoodsDetailEntity cGoodsDetail){
		ValidatorUtils.validateEntity(cGoodsDetail, UpdateGroup.class);
		cGoodsDetail.setGDDatastate("修改");
		cGoodsDetailService.update(cGoodsDetail);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		cGoodsDetailService.deleteBatch(guids);
		
		return R.ok();
	}
	
}
